from itsdangerous import BadData
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from django.conf import settings
# from apps.oauth import constants
#加密
def generate_access_token(user_id):
    """
    :param user_id: 用户的id
    :return: access_token
    """
    s = Serializer(settings.SECRET_KEY, expires_in=3600)
    data = {'user_id': user_id}
    token = s.dumps(data)
    return token.decode()
#解密
def check_access_token(token):
    """
    提取user_id
    :param access_token: 签名后的openid
    :return: openid or None
    """
    s = Serializer(settings.SECRET_KEY, expires_in=3600)
    try:
        data = s.loads(token)
    except BadData:
        return None
    else:
        return data.get('user_id')